#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;
const int INF = 1e9;

int n, m;
struct Node {
  int x, p;
  bool operator<(Node a) const { return x < a.x; }
} a[N];
int f[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i].x >> a[i].p;
  sort(a + 1, a + n + 1);
  int ans = 0;
  rep(i, 1, n) {
    f[i][i] = a[i].p;
    ans = max(ans, f[i][i]);
  }
  rep(i, 1, n) {
    deque<int> dq;
    int pos = i;
    rep(j, i + 1, n) {
      while (pos && a[i].x - a[pos].x <= a[j].x - a[i].x) {
        while (dq.size() && f[i][dq.back()] <= f[i][pos]) dq.pop_back();
        dq.push_back(pos--);
      }
      f[j][i] = f[i][dq.front()] + a[j].p;
      ans = max(ans, f[j][i]);
    }
  }
  per(i, n, 1) {
    deque<int> dq;
    int pos = i;
    per(j, i - 1, 1) {
      while (pos <= n && a[pos].x - a[i].x <= a[i].x - a[j].x) {
        while (dq.size() && f[i][dq.back()] <= f[i][pos]) dq.pop_back();
        dq.push_back(pos++);
      }
      f[j][i] = f[i][dq.front()] + a[j].p;
      ans = max(ans, f[j][i]);
    }
  }
  cout << ans;
  return 0;
}